#!/usr/bin/env ruby

require 'json'

def parseOptions(paired_args)
    options = {args: []}
    index = 0
    while(index < ARGV.length)
        arg = ARGV[index]

        paired_key = paired_args[arg]
        if paired_key
            if index >= ARGV.length - 1
                puts "Unable to find a value for the arg: '" + arg + "'"
                exit 1
            end
            options[paired_key] = ARGV[index+1]
            index += 1
        elsif /--.+/ =~ arg
            puts "unknown option: " + arg
        else
            options[:args].push arg
        end
        index += 1
    end
    options
end

def requireArgs(args, count)
    if args.length != count
        puts "usage: docgen [input] [output] [...options]"
        exit 1
    end
end

def splitSections(content)
    result = {}
    current = ''
    content.split("\n").each do |line|
        if /^#( | \w).*/ =~ line
            current = line.sub(/^#/, '').strip
            # current = line
            result[current] = result[current] || ""
        else
            result[current] += "\n" + line
        end
    end

    result.keys.each do |key|
        result[key] = result[key].strip
    end
    result
end

paired_args = {
    '--section' => :section,
    '--title' => :title,
    '--transforms' => :transforms
}
options = parseOptions paired_args

requireArgs options[:args], 2
file_in = options[:args][0]
file_out = options[:args][1]

input = IO.read(file_in)

result = ''

# -----

if options[:section]
    sections = splitSections input
    section_name = options[:section]
    if sections[section_name]
        result = sections[section_name]
    else
        puts "Unable to find section: '" + section_name + "'"
        exit 1
    end
else
    result = input
end


if options[:title]
    result = "# #{options[:title]}\n\n" + result
end

# -----

jazzy_conf = JSON.parse IO.read '.jazzy.json'

if jazzy_conf['string-replacements']
    transforms = jazzy_conf['string-replacements']
    transforms.each_pair do |key, value|
        result = result.sub key, value
    end
end

version = `cat ReSwift.podspec| grep "version " | cut -d '"' -f 2`.strip
if version
    result = result.sub '{{version}}', version
end

# -----

if result == ''
    puts "Not writing output as result is empty"
    exit 1
end

IO.write(file_out, result)

puts "[Processed]: #{file_out}"
